package com.lxu.util;

import com.lxu.entity.WordEntity;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.xwpf.usermodel.*;

import java.util.List;
import java.util.Map;

public class XWPFDocumentTableUtils {

    /**
     * 替换表格对象方法
     *
     * @param document   docx解析对象
     * @param wordEntity textMap 需要替换的信息集合
     * @param wordEntity extendTableMap 需要插入的表格信息集合
     */
    public static void changeTableProvince(XWPFDocument document, WordEntity wordEntity) {
        //获取表格对象集合
        List<XWPFTable> tables = document.getTables();
        int tableNo = 0;
        for (int i = 0; i < tables.size(); i++) {
            //只处理行数大于等于2的表格，且不循环表头
            XWPFTable table = tables.get(i);
            if (table.getRows().size() > 1) {
                //判断表格是需要替换还是需要插入，判断逻辑有$为替换，表格无$为插入
                if (XWPFDocumentBaseUtils.checkText(table.getText())) {
                    List<XWPFTableRow> rows = table.getRows();
                    //遍历表格,并替换模板
                    eachTable(rows, wordEntity.getTextMap());
                    tableNo++;
                } else {
                    List<List<String>> tableList = wordEntity.getTableMap().get("table" + tableNo);
                    insertTable(table, wordEntity.getStartRow(), wordEntity.getRows(), tableList);
                    tableNo++;
                }
            }
        }
    }

    /**
     * 遍历表格
     *
     * @param rows    表格行对象
     * @param textMap 需要替换的信息集合
     */
    public static void eachTable(List<XWPFTableRow> rows, Map<String, Object> textMap) {
        for (XWPFTableRow row : rows) {
            List<XWPFTableCell> cells = row.getTableCells();
            for (XWPFTableCell cell : cells) {
                //判断单元格是否需要替换
                if (XWPFDocumentBaseUtils.checkText(cell.getText())) {
                    List<XWPFParagraph> paragraphs = cell.getParagraphs();
                    for (XWPFParagraph paragraph : paragraphs) {
                        List<XWPFRun> runs = paragraph.getRuns();
                        for (XWPFRun run : runs) {
                            run.setText(XWPFDocumentBaseUtils.changeValue(run.toString(), textMap), 0);
                        }
                    }
                }
            }
        }
    }

    /**
     * 为表格插入数据，行数不够添加新行
     *
     * @param table     需要插入数据的表格
     * @param tableList 插入数据集合
     */
    public static void insertTable(XWPFTable table, List<List<String>> tableList) {
        //创建行,根据需要插入的数据添加新行，不处理表头
        for (int i = 1; i < tableList.size(); i++) {
            //XWPFTableRow row =table.createRow();
        }
        //遍历表格插入数据
        List<XWPFTableRow> rows = table.getRows();
        for (int i = 1; i < rows.size(); i++) {
            XWPFTableRow newRow = table.getRow(i);
            List<XWPFTableCell> cells = newRow.getTableCells();
            for (int j = 0; j < cells.size(); j++) {
                XWPFTableCell cell = cells.get(j);
                XWPFParagraph xwpfParagraph = cell.getParagraphs().get(0);//获取cell中的第一个段落第一个ruan并设置字体样式和内容
                XWPFRun run = xwpfParagraph.createRun();
                run.setFontSize(9);
                run.setFontFamily("宋体");
                run.setText(tableList.get(i - 1).get(j));
            }
        }
    }

    public static void insertTable(XWPFTable table, int startRow, int totalRows, List<List<String>> tableList) {
        if(CollectionUtils.isEmpty(tableList)){
            return;
        }
        //创建行,根据需要插入的数据添加新行，不处理表头
        for (int i = 1; i < tableList.size(); i++) {
            //XWPFTableRow row =table.createRow();
        }
        //遍历表格插入数据
        List<XWPFTableRow> rows = table.getRows();
        for (int i = startRow; i < totalRows + startRow; i++) {
            XWPFTableRow newRow = table.getRow(i);
            List<XWPFTableCell> cells = newRow.getTableCells();
            for (int j = 0; j < cells.size(); j++) {
                XWPFTableCell cell = cells.get(j);
                List<XWPFParagraph> paragraphs = cell.getParagraphs();
                for (XWPFParagraph paragraph : paragraphs) {
                    List<XWPFRun> runs = paragraph.getRuns();
                    for (XWPFRun run : runs) {
                        run.setText(tableList.get(i - startRow).get(j),0);
                    }
                }
/*
                XWPFParagraph xwpfParagraph = cell.getParagraphs().get(0);//获取cell中的第一个段落第一个ruan并设置字体样式和内容
                XWPFRun temp = CollectionUtils.isEmpty(xwpfParagraph.getRuns())?null:xwpfParagraph.getRuns().get(0);
                int fontSize =9;
                String fontFamily="宋体";
                if(temp!=null){
                    fontSize = temp.getFontSize();
                    fontFamily = temp.getFontFamily();
                }
                XWPFRun run = xwpfParagraph.createRun();
                run.setFontSize(fontSize);
                run.setFontFamily(fontFamily);
                run.setText(tableList.get(i - startRow).get(j));
*/
            }
        }
    }
}
